//
//  JLButtomTabBar.m
//  LIangeNews
//
//  Created by sun on 16/1/14.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLButtomTabBar.h"
#import "JLButtonTabBarButton.h"
@interface JLButtomTabBar()
@property(nonatomic,strong) UIButton * selectedButton;
@end
@implementation JLButtomTabBar
-(void)addButtomTabBarButtonWithNorImage:(NSString *)norImage seleImage:(NSString *)seleImage title:(NSString *)title{
    JLButtonTabBarButton * btn = [[JLButtonTabBarButton alloc]init];
    [btn setImage:[UIImage imageNamed:norImage] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:seleImage] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(didClickTabBarButton:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];
}

-(void)didClickTabBarButton:(JLButtonTabBarButton *)btn{
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton = btn;
    if ([self.delegate respondsToSelector:@selector(buttomTabBar:didClickButtomTabBarWithIndex:)]) {
        [self.delegate buttomTabBar:self didClickButtomTabBarWithIndex:(int)btn.tag];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    NSUInteger count = self.subviews.count;
    CGFloat width = self.bounds.size.width / count;
    CGFloat height = self.bounds.size.height;
    CGFloat y = 0;
    for (int i = 0; i < count ; i++) {
        JLButtonTabBarButton * btn = self.subviews[i];
        CGFloat x = width * i;
        btn.frame = CGRectMake(x, y, width, height);
        btn.tag = i;
        if (i == 0) {
            self.selectedButton = btn;
            btn.selected = YES;
            
        }
    }
    
}
@end
